package com.fix.beans;


import java.io.Serializable;
import java.io.UnsupportedEncodingException;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.servlet.http.HttpServletRequest;

import com.fix.model.Data;
import com.fix.model.Guide;
import com.fix.model.Line;
import com.fix.model.Media;
import com.fix.model.Response;
import com.fix.model.Result;
import com.fix.model.Step;
import com.fix.model.Translated;
import com.fix.util.FixClientController;
import com.google.gson.Gson;

@ManagedBean(name="search")
@RequestScoped
public class Search implements Serializable {
	private static final long serialVersionUID = 1L;	
	public String info = "Guides integration.";
	public Result[] results; 
	public String search;
	public int guideId;
	public Guide guide;
	
	public Search() {
	}

	public String getInfo() {
		return info;
	}
	public void setInfo(String info) {
		this.info = info;
	}
	
	public Result[] getResults() {
		return results;
	}
	public String getSearch() {
		return search;
	}
	public void setSearch(String search) {
		this.search = search;
		search();
	}
	public int getGuideId() {
		return guideId;
	}
	public void setGuideId(int guideId) {
		findGuide();
		this.guideId = guideId;
	}
	public Guide getGuide() {
		return guide;
	}
	public void setGuide(Guide guide) {
		this.guide = guide;
	}
	
	private void search() {
		final FixClientController fixClientController = new FixClientController();
		Gson gson = new Gson();
		String json = "";
		try {
			json = fixClientController.search(search);
			Response response = gson.fromJson(json, Response.class);			
			System.out.println("search: " + response.getSearch() + " getTotalResults: " + response.getTotalResults());
			results = response.getResults();
//			for (int i = 0; i < results.length; i++) {
//				System.out.println(results[i].getTitle() + " " + results[i].getGuideid());
//			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	
	private void findGuide() {
		final FixClientController fixClientController = new FixClientController();
		Gson gson = new Gson();
		String json = "";
		try {
			json = fixClientController.getGuide(guideId);
			guide = gson.fromJson(json, Guide.class);
			
			System.out.println(guide.getTitle());
			Step[] steps = guide.getSteps();
			for (int j = 0; j < steps.length; j++) {
				System.out.println("Step # " + steps[j].getOrderby());
				Line[] lines = steps[j].getLines();
				for (int k = 0; k < lines.length; k++) {
					String text_raw = lines[k].getText_raw();
					String jsonfromyadex = fixClientController.translate(text_raw);
					Translated tr = gson.fromJson(jsonfromyadex, Translated.class);
					System.out.println("  " + lines[k].getLevel() + " " + tr.getText()[0] );
				}
				Media media = steps[j].getMedia();
				Data[] data = media.getData();
				for (int l = 0; l < data.length; l++) {
					System.out.println("image: " + data[l].getStandard());
				}
			}
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
	}
	
	private String getParamValue(String param) {
		HttpServletRequest req = (HttpServletRequest)FacesContext.getCurrentInstance().getExternalContext().getRequest();
		final String p = req.getParameter(param);
		return p;
	}
	
}
